package 笔试.去哪儿;

import java.util.ArrayList;
import java.util.Scanner;

public class 字典序排数组 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = sc.nextInt();
        }
        ArrayList<Integer> cur = new ArrayList<>();
        StringBuilder curStr = new StringBuilder();
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        String resStr = "";
        boolean[] visited = new boolean[n];
        getResult(nums, cur, curStr, visited, res, resStr);
        ArrayList<Integer> list = res.get(0);
        for (int i = 0; i < list.size(); i++) {
            if (i != list.size() - 1) {
                System.out.printf("%d ", list.get(i));
            } else {
                System.out.printf("%d\n", list.get(i));
            }
        }
    }

    private static void getResult(int[] nums, ArrayList<Integer> cur, StringBuilder curStr, boolean[] visited, ArrayList<ArrayList<Integer>> res, String resStr) {
        if (cur.size() == nums.length) {
            if (res.isEmpty()) {
                resStr = curStr.toString();
                res.add(new ArrayList<>(cur));
            } else {
                if (compare(curStr.toString(), resStr)) {
                    res.remove(0);
                    res.add(new ArrayList<>(cur));
                    resStr = curStr.toString();
                }
            }
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!visited[i]) {
                visited[i] = true;
                cur.add(nums[i]);
                curStr.append(nums[i]);
                getResult(nums, cur, curStr, visited, res, resStr);
                cur.remove(cur.size() - 1);
                curStr.deleteCharAt(curStr.length() - 1);
                visited[i] = false;
            }

        }

    }

    public static boolean compare(String a, String b) {
        if (b.startsWith(a) && !b.equals(a)) {
            return true;
        }
        int i = 0;
        while (i < a.length() && i < b.length()) {
            char cha = a.charAt(i);
            char chb = b.charAt(i);
            if (cha != chb) {
                return cha < chb;
            }
            i++;
        }
        return false;
    }


}